// 辅助函数

// 辅助函数：格式化日期
function formatDate(dateString, isRegistrationDate = false) {
    if (!dateString) return '暂无日期信息';
    
    const date = new Date(dateString);
    const now = new Date();
    
    // 如果是注册时间且在当前时间之前注册的，统一显示为2025-5-16
    if (isRegistrationDate && date < now) {
        return '2025-05-16 00:00:00';
    }
    
    return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false
    });
}

// 辅助函数：格式化地址
function formatAddress(addressInfo) {
    if (!addressInfo) return '暂无地址信息';
    const name = addressInfo.name || '未提供姓名';
    const phone = addressInfo.phone || '未提供电话';
    const address = addressInfo.detailAddress || '未提供详细地址';
    return `${name} ${phone}<br>${address}`;
}

// 辅助函数：获取订单状态对应的徽章类
function getOrderStatusBadgeClass(status) {
    switch (status) {
        case 'PENDING':
            return 'bg-secondary';
        case 'PENDING_COLLECTION':
            return 'bg-info';
        case 'COLLECTED':
            return 'bg-primary';
        case 'IN_TRANSIT':
            return 'bg-primary';
        case 'OUT_FOR_DELIVERY':
            return 'bg-warning';
        case 'DELIVERED':
            return 'bg-success';
        case 'SIGNED':
            return 'bg-success';
        case 'COMPLETED':
            return 'bg-success';
        case 'CANCELLED':
            return 'bg-danger';
        case 'DELIVERY_FAILED':
            return 'bg-danger';
        default:
            return 'bg-secondary';
    }
}

// 辅助函数：获取配送员状态对应的徽章类
function getStatusBadgeClass(status) {
    switch (status) {
        case 'IDLE':
            return 'bg-info';
        case 'DELIVERING':
            return 'bg-primary';
        case 'BUSY':
            return 'bg-warning';
        case 'OFFLINE':
            return 'bg-secondary';
        default:
            return 'bg-secondary';
    }
}

// 辅助函数：获取配送员状态对应的指示器类
function getStatusIndicatorClass(status) {
    switch (status) {
        case 'IDLE':
            return 'status-idle';
        case 'DELIVERING':
            return 'status-online';
        case 'BUSY':
            return 'status-busy';
        case 'OFFLINE':
            return 'status-offline';
        default:
            return 'status-offline';
    }
}

// 辅助函数：格式化订单状态
function formatOrderStatus(status) {
    switch (status) {
        case 'PENDING':
            return '待处理';
        case 'PENDING_PAYMENT':
            return '待支付';
        case 'PENDING_COLLECTION':
            return '待揽收';
        case 'COLLECTED':
            return '已揽收';
        case 'IN_TRANSIT':
            return '运输中';
        case 'TRANSPORTING':
            return '运输中';
        case 'OUT_FOR_DELIVERY':
            return '派送中';
        case 'DELIVERED':
            return '已送达';
        case 'SIGNED':
            return '已签收';
        case 'COMPLETED':
            return '已完成';
        case 'CANCELLED':
            return '已取消';
        case 'DELIVERY_FAILED':
            return '派送失败';
        case 'REFUNDED':
            return '已退款';
        default:
            return status || '未知状态';
    }
}

// 辅助函数：格式化配送员状态
function formatDeliveryPersonStatus(status) {
    switch (status) {
        case 'IDLE':
            return '空闲';
        case 'DELIVERING':
            return '配送中';
        case 'BUSY':
            return '忙碌';
        case 'OFFLINE':
            return '离线';
        default:
            return status || '未知状态';
    }
}

// 辅助函数：显示提示信息
function showAlert(message, type = 'info') {
    alertArea.innerHTML = `
        <div class="alert alert-${type} alert-dismissible fade show" role="alert">
            ${message}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    `;
    alertArea.style.display = 'block';

    // 5秒后自动关闭
    setTimeout(() => {
        const alert = alertArea.querySelector('.alert');
        if (alert) {
            const bsAlert = new bootstrap.Alert(alert);
            bsAlert.close();
        }
    }, 5000);
}

// 辅助函数：显示/隐藏加载指示器
function showLoader(show) {
    mainLoader.style.display = show ? 'flex' : 'none';
}

// 辅助函数：带认证的API请求
async function fetchAuthenticatedApi(url, options = {}) {
    const defaultOptions = {
        headers: {
            'Content-Type': 'application/json'
        },
        credentials: 'same-origin'
    };

    const mergedOptions = {
        ...defaultOptions,
        ...options,
        headers: {
            ...defaultOptions.headers,
            ...options.headers
        }
    };

    return fetch(url, mergedOptions);
}

// 辅助函数：获取下一个可能的订单状态
function getNextPossibleStatuses(currentStatus) {
    switch (currentStatus) {
        case 'PENDING':
            return ['PENDING_COLLECTION'];
        case 'PENDING_COLLECTION':
            return ['COLLECTED'];
        case 'COLLECTED':
            return ['TRANSPORTING', 'IN_TRANSIT'];
        case 'TRANSPORTING':
        case 'IN_TRANSIT':
            return ['OUT_FOR_DELIVERY'];
        case 'OUT_FOR_DELIVERY':
            return ['DELIVERED', 'DELIVERY_FAILED'];
        case 'DELIVERED':
            return ['SIGNED'];
        case 'SIGNED':
            return ['COMPLETED'];
        case 'COMPLETED':
            return [];
        case 'DELIVERY_FAILED':
            return ['PENDING_COLLECTION', 'CANCELLED'];
        case 'CANCELLED':
            return [];
        default:
            return [];
    }
}

// 播放提示音函数
function playNotificationSound() {
    try {
        const audio = new Audio('/sounds/notification.mp3');
        audio.play();
    } catch (e) {
        console.warn('无法播放提示音:', e);
    }
}